home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / proff.zoo / pxxparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-04  |  9.5 KB  |  518 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "proff.h"
  4. #include "debug.h"
  5. #include "lextab.h"
  6.  
  7. #define brk brrk
  8.  
  9. #define RUNOFF    1         /* recognise RUNOFF commands */
  10.  
  11. char literal = NO;        /* literal flag             */
  12.  
  13. /*
  14.  * command - perform formatting command
  15.  *
  16.  */
  17. command(buf)
  18. char buf[];
  19. {
  20.     char token[MAXTOK], xtoken[MAXTOK], variable[MAXTOK], *defn;
  21.     char special[MAXTOK];
  22.     int argtyp, ct, at, spval, i, flags;
  23.     register int val, n, rest;
  24.     char onflag = FALSE; 
  25.     char offlag = FALSE;
  26.     struct lexlist *xp;
  27. #ifdef __GNUC__
  28.     void *malloc(size_t);
  29. #else
  30.     char *malloc();
  31. #endif
  32.     dovar(tbuf1,buf);    /* use scratch buffer to expand variables */
  33.     strcpy(buf,tbuf1);
  34.  
  35.     i = 1;
  36.     n = getwrd(buf, &i, token);    /* get the command token */
  37.     rest = i;            /* remaining string */
  38.     ct = comtype(token, n, &defn, &flags);
  39.     if (ct == UNKNOWN)
  40.         return;
  41.     if (literal && ct != ELT) {    /* ignore while literal  */
  42.         put(buf);
  43.         return;
  44.     }
  45.  
  46. #ifdef DOUBLEWORD
  47.     if (flags == 2) {    /* check for 2-word command */
  48.         n = getwrd(buf, &i, xtoken);
  49.         if (n == 0) {
  50.             fprintf(stderr,"%c%s what ?\n", cchar, token);
  51.             return;
  52.         }
  53.         if ((at = comtype(xtoken, n, &defn, &flags)) == UNKNOWN) {
  54.             fprintf(stderr,"%c%s %s unknown.\n", cchar,
  55.             token,
  56.             xtoken);
  57.             return;
  58.         }
  59.         else
  60.             ct += at;
  61.     }
  62. #endif
  63.  
  64.     doesc(buf, variable, MAXLINE);    /* expand escapes */
  65.     n = getarg(buf, &i, xtoken);    /* first parameter*/
  66.     argtyp = '\n';    /* defaulted ** cludge ** */
  67.     val = 0;
  68.     if (n > 0) {
  69.         if (*xtoken == '+' || *xtoken == '-') {
  70.             argtyp = *xtoken;
  71.             val = atoi(xtoken+1);
  72.         }
  73.         else if (isdigit(*xtoken)) {
  74.             argtyp = 0;
  75.             val = atoi(xtoken);
  76.         }
  77.         else {
  78.             /* check some common flags */
  79.             if (strcmp("on",xtoken) == 0)
  80.                 onflag = TRUE;
  81.             else if (strcmp("off",xtoken) == 0)
  82.                 offlag = TRUE;
  83.         }
  84.     }
  85.  
  86.     switch(ct) {
  87.  
  88.     case MACRO:
  89.         eval(buf, defn);
  90.         break;
  91.     case FI:
  92.         brk();
  93.         fill = YES;
  94.         break;
  95.     case NF:
  96.         brk();
  97.         fill = NO;
  98.         break;
  99.     case BR:
  100.         brk();
  101.         break;
  102.     case LS:
  103.         set(&lsval, val, argtyp, 1, 1, HUGE);
  104.         break;
  105.     case CE:
  106.         brk();
  107.         if (onflag)
  108.             CEon = TRUE;
  109.         else if (offlag) {
  110.             CEon = FALSE;
  111.             ceval = 0;        /* reset */
  112.         }
  113.         else
  114.             set(&ceval, val, argtyp, 1, 0, HUGE);
  115.         break;
  116.     case UL:
  117.         if (onflag) {
  118.             ULon = TRUE;
  119.             break;
  120.         }
  121.         else if (offlag) {
  122.             ULon = FALSE;
  123.              ulval = 0;        /* reset */
  124.             break;
  125.         }
  126.         else 
  127.             set(&ulval, val, argtyp, 0, 1, HUGE);
  128.  
  129.         if (!isdigit(*xtoken)) {
  130.             if (strcmp("all",xtoken) == 0) {
  131.                     ulblnk = '_';
  132.                     ulval = 0;
  133.             }
  134.             else if (strcmp("words",xtoken) == 0) {
  135.                 ulblnk = ' ';
  136.                 ulval = 0;
  137.             }
  138.         }
  139.         break;
  140.     case BD:
  141.         if (bolding == YES) {
  142.             if (onflag)
  143.                 BDon = TRUE;
  144.             else if (offlag) {
  145.                 BDon = FALSE;
  146.                 boval = 0;    /* reset */
  147.             }
  148.             else
  149.                 set(&boval, val, argtyp, 0, 1, HUGE);
  150.         }
  151.         break;
  152.     case HE:
  153.         gettl(buf, ehead, ehlim);
  154.         gettl(buf, ohead, ohlim);
  155.         break;
  156.     case FO:
  157.         gettl(buf, efoot, eflim);
  158.         gettl(buf, ofoot, oflim);
  159.         break;
  160.     case BP:
  161.         if (paging == NO)
  162.             break;
  163.         brk();
  164.         if (lineno > 0)
  165.             space(HUGE);
  166.         set(&curpag, val, argtyp, curpag+1, -HUGE, HUGE);
  167.         newpag = curpag;
  168.         break;
  169.     case SP:
  170.         set(&spval, val, argtyp, 1, 0, HUGE);
  171.         space(spval);
  172.         break;
  173.     case IN:
  174.         brk();
  175.         set(&inval, val, argtyp, 0, 0, rmval-1);
  176.         tival = inval;
  177.         break;
  178.     case RM:
  179.         set(&rmval, val, argtyp, PAGEWIDTH, tival+1, HUGE);
  180.         break;
  181.     case TI:
  182.         brk();
  183.         set(&tival, val, argtyp, 0, 0, rmval);
  184.         break;
  185.     case LEX:    /****/
  186.         if ((xp = Remove(xtoken,lextab)) != NULL) {
  187.             if (getwrd(buf, &i, variable) != 0)
  188.                 lexinstal(variable,xp->val,xp->flag,lextab);
  189.         }
  190.         else 
  191.             fprintf(stderr,"%s undefined.\n",xtoken);
  192.         break;
  193.     case PN:    /****/
  194.         if (strcmp(xtoken,"roman") == 0)
  195.             roman = TRUE;
  196.         else if (strcmp(xtoken,"arabic") == 0)
  197.             roman = FALSE;
  198.         else
  199.             fprintf(stderr,"%c%s does not have %s option.\n",
  200.             cchar,token,xtoken);
  201.         break;
  202.     case IG:    /****/
  203.         break;
  204.     case SET:    /****/
  205.         if (n > 0) {
  206.             if (isdigit(*xtoken)) {
  207.                 fprintf(stderr,"illegal variable name %s\n",
  208.                 xtoken);
  209.                 break;
  210.             }
  211.             *variable = '\0';
  212.             n = getarg(buf, &i, variable);
  213.             if (n <= 0) {
  214.                 fprintf(stderr,"%s: ", xtoken);
  215.                 gets(variable);
  216.  
  217.             }
  218.             if (*variable != '\0')
  219.                 install(xtoken, variable, gentab);
  220.         }
  221.         else
  222.             fprintf(stderr,"%c%s needs a variable name.\n",
  223.             cchar, token);
  224.         break;
  225.     case GET:    /****/
  226.         if (n > 0) {
  227.             if (isdigit(*xtoken)) {
  228.                 fprintf(stderr,"illegal variable name %s\n",
  229.                 xtoken);
  230.                 break;
  231.             }
  232.             *variable = '\0';
  233.             n = getarg(buf, &i, tbuf3); /* using temp buf3 */
  234.             if (n > 0) {
  235.                 fprintf(stderr,"%s", tbuf3);
  236.                 gets(variable);
  237.  
  238.             }
  239.             if (*variable != '\0')
  240.                 install(xtoken,variable, gentab);
  241.         }
  242.         else
  243.             fprintf(stderr,"%c%s needs a variable name.\n",
  244.             cchar, token);
  245.         break;
  246.     case CL:    /****/
  247.         if (argtyp == '\n') {
  248.             clast->level = 0;
  249.             clast->str = NULL;
  250.         }
  251.         else {
  252.             skipbl(buf,&i);
  253.             if (*(buf+i) == '\0')
  254.                 break;        /* no contents line here ! */
  255.             clast->level = val * 3; /* level * indent        */
  256.             n = i;
  257.             while(*(buf+n) != '\n')
  258.                 n++;
  259.             *(buf+n) = '\0';    /* destroy CR with a null  */
  260.             clast->str = strsave(buf+i);
  261.             clast->page = curpag;
  262.         }
  263.         clast->nextc = (struct clist *) malloc((size_t)sizeof(struct clist));
  264.         p_memoryus += sizeof(struct clist);
  265.         clast = clast->nextc;
  266.         clast->nextc = NULL;
  267.         break;
  268.     case PC:    /****/
  269.         brk();
  270.         clast = chead;
  271.         while(clast->nextc != NULL) {
  272.             if (clast->str == NULL)
  273.                 put("\n");
  274.             else {
  275.                 tival = (int) clast->level + inval;
  276.                 i = rmval - tival;
  277.                 docline(variable, i, clast->str, clast->page);
  278.                 put(variable);
  279.             }
  280.             clast = clast->nextc;
  281.         }
  282.         break;
  283.     case DBO:    /****/
  284.         bolding = NO;
  285.         break;
  286.     case EBO:    /****/
  287.         bolding = YES;
  288.         break;
  289.     case AP:    /****/
  290.         autopar = YES;
  291.         break;
  292.     case NAP:    /****/
  293.         autopar = NO;
  294.         break;
  295.     case SAV:    /****/
  296.         brk();
  297.         save();
  298.         break;
  299.     case RST:    /****/
  300.         brk();
  301.         restore();
  302.         break;
  303.     case NPA:    /****/
  304.         paging = NO;
  305.         savpl = plval;
  306.         plval = HUGE;
  307.         bottom = plval - m3val - m4val;
  308.         break;
  309.     case PGI:    /****/
  310.         bottom = lineno - 1;    /* force end-of-page */
  311.         brk();
  312.         plval = savpl;
  313.         break;
  314.     case LTR:    /****/
  315.         brk();
  316.         if (save()) {
  317.             inval = 0;
  318.             rmval = 132;
  319.             autopar = NO;
  320.             lsval = 0;
  321.             fill = NO;
  322.             literal = YES;
  323.         }
  324.         break;
  325.     case ELT:    /****/
  326.         restore();
  327.         literal = NO;
  328.         break;
  329.     case WR:    /****/
  330.         brk();
  331.         getpstr(buf+rest,special);
  332.         defn = special;
  333.         while(*defn)
  334.             putchar(*defn++);
  335.         break;
  336.     case PL:
  337.         if (paging == NO)
  338.             break;
  339.         set(&plval, val, argtyp, PAGELEN,
  340.         m1val + m2val + m3val + m4val + 1, HUGE);
  341.         bottom = plval - m3val - m4val;
  342.         break;
  343.     case PO:
  344.         set(&offset, val, argtyp, 0, 0, rmval - 1);
  345.         break;
  346.     case M1:
  347.         set(&m1val, val, argtyp, 3, 0,
  348.         plval - m2val - m3val - m4val - 1);
  349.         break;
  350.     case M2:
  351.         set(&m2val, val, argtyp, 2, 0,
  352.         plval - m1val - m3val - m4val - 1);
  353.         break;
  354.     case M3:
  355.         set(&m3val, val, argtyp, 2, 0,
  356.         plval - m1val - m2val - m4val - 1);
  357.         bottom = plval - m3val - m4val;
  358.         break;
  359.     case M4:
  360.         set(&m4val, val, argtyp, 3, 0,
  361.         plval - m1val - m2val - m3val - 1);
  362.         bottom = plval - m3val - m4val;
  363.         break;
  364.     case EH:
  365.         gettl(buf, ehead, ehlim);
  366.         break;
  367.     case OH:
  368.         gettl(buf, ohead, ohlim);
  369.         break;
  370.     case EF:
  371.         gettl(buf, efoot, eflim);
  372.         break;
  373.     case OF:
  374.         gettl(buf, ofoot, oflim);
  375.         break;
  376.     case CC:
  377.         cchar = *xtoken;
  378.         if (cchar == '\0' || cchar == '\n')
  379.             cchar = '.';
  380.         if ((lineno + val) > bottom && lineno <= bottom) {
  381.             space(val);
  382.             lineno = 0;
  383.         }
  384.         break;
  385.     case EC:
  386.         genesc = *xtoken;
  387.         if (genesc == '\0' || genesc == '\n')
  388.             genesc = '_';
  389.         break;
  390.     case NE:
  391.         if ((lineno + val) > bottom && lineno <= bottom) {
  392.             space(val);
  393.             lineno = 0;
  394.         }
  395.         break;
  396.     case BS:
  397.         set(&bsval, val, argtyp, 1, 0, HUGE);
  398.         break;
  399.     case JU:
  400.         rjust = YES;
  401.         break;
  402.     case NJ:
  403.         rjust = NO;
  404.         break;
  405.     case SO:
  406.         if (n <= 0)
  407.             return;
  408.         if (level + 1 == NFILES)
  409.             error("? SO commands nested too deeply.");
  410.         if ((infile[level + 1] = fopen(xtoken, "r")) != NULL) {
  411.             level++;
  412.             if (verbose == YES)
  413. #ifdef rainbow
  414.                 fprintf(stderr,"source \033[7m%s\033[0m\n",
  415.                     xtoken);
  416. #else
  417.                 fprintf(stderr,"source %s\n",xtoken);
  418. #endif
  419.         }
  420.         else
  421.             fprintf(stderr,"%s: cannot open.\n",xtoken);
  422.         break;
  423.     case OU:    /*****/
  424.         /* skip for now. */
  425.         break;
  426.  
  427.     case OE:    /*****/
  428.         /* skip for now. */
  429.         break;
  430.  
  431.     case CU:
  432.         ulblnk = '_';
  433.         set(&ulval, val, argtyp, 0, 1, HUGE);
  434.         break;
  435.     case DE:
  436.  
  437. #ifdef DEBUG
  438. printf("Command++: calling dodef Fp %ld\n",infile[level]);
  439. #endif
  440.         dodef(buf, infile[level]);
  441.         break;
  442.     case NR:
  443.         if (n <= 0)
  444.             return;
  445.         if (*xtoken < 'a' || *xtoken > 'z')
  446.             error("invalid number register [%c].",*xtoken);
  447.  
  448.         val = getval(buf, &i, &argtyp);
  449.         set(&nr[xtoken[0] - 'a'], val, argtyp, 0, -HUGE, HUGE);
  450.         break;
  451.     case ST:
  452.         if (argtyp == '-')
  453.             spval = plval;
  454.         else
  455.             spval = 0;
  456.         set(&spval, val, argtyp, 0, 1, bottom);
  457.         if (spval > lineno && lineno == 0)
  458.             phead();
  459.         if (spval > lineno)
  460.             space(spval - lineno);
  461.         break;
  462.     case RESET:    /****/
  463.         finit();
  464.         break;
  465.     default:
  466.         error("? Botch in command.");
  467.         break;
  468.     }
  469. }
  470.  
  471. /*
  472.  * comtype - decode the command type
  473.  *
  474.  */
  475. int
  476. comtype(buf, siz, defn, flags)
  477. char buf[];
  478. int siz;
  479. char **defn;
  480. int *flags;
  481. {
  482.  
  483.     struct hashlist *np;
  484.     struct lexlist *xp;
  485.     extern    struct lexlist *lexlook();
  486.     int i,comtyp;
  487.     char c1,c2;
  488.  
  489.  
  490. #ifdef DEBUG
  491.     printf("comtype:  (token)\n");
  492. #endif
  493.  
  494.     if ((np = lookup(buf, macrotab)) != NULL) {
  495.         *defn=np->def;
  496.         return(MACRO);
  497.     }
  498.     comtyp = UNKNOWN;
  499.  
  500.     if (*buf == '#' || *buf == '!')
  501.         return(comtyp);
  502.  
  503.     if ((xp = lexlook(buf,lextab)) != NULL)
  504.         if (onlyrunoff && (xp->flag != RUNOFF)) {
  505.             fprintf(stderr,"%c%s is not a runoff command.\n",
  506.             cchar,buf);
  507.             return(UNKNOWN);
  508.         }
  509.         else {    
  510.             comtyp = xp->val;
  511.             *flags = xp->flag;
  512.         }
  513.  
  514.     if (comtyp == UNKNOWN)
  515.         fprintf(stderr,"unknown command %c%s\n",cchar,buf);
  516.     return(comtyp);
  517. }
  518.